package main

import (
	"log"
	"strings"

	"github.com/inntu/backend/config"
	"github.com/inntu/backend/api/models"
)

func main() {
	// 初始化数据库连接
	dbConfig := config.GetDatabaseConfig()
	err := config.InitGormDatabase(dbConfig)
	if err != nil {
		log.Fatal("初始化数据库失败:", err)
	}
	defer config.CloseGormDatabase()

	db := config.GetGormDB()
	if db == nil {
		log.Fatal("无法连接到数据库")
	}

	// 查询几个示例记录验证content字段
	testEndpoints := []string{"forum", "calendar", "map", "weather", "about"}

	log.Println("=== 验证content字段更新结果 ===")

	for _, endpoint := range testEndpoints {
		var item models.Item
		err := db.Where("api_endpoint = ?", endpoint).First(&item).Error
		if err != nil {
			log.Printf("❌ %s: 查询失败 - %v", endpoint, err)
			continue
		}

		if item.Content == nil || *item.Content == "" {
			log.Printf("❌ %s: content字段为空", endpoint)
		} else {
			contentPreview := *item.Content
			if len(contentPreview) > 50 {
				contentPreview = contentPreview[:50] + "..."
			}
			// 替换换行符以便显示
			contentPreview = strings.ReplaceAll(contentPreview, "\n", "\\n")
			log.Printf("✅ %s (ID: %d): %s", endpoint, item.ID, contentPreview)
		}
	}

	// 统计有content的记录数量
	var totalCount int64
	var withContentCount int64

	db.Model(&models.Item{}).Count(&totalCount)
	db.Model(&models.Item{}).Where("content IS NOT NULL AND content != ''").Count(&withContentCount)

	log.Printf("\n=== 统计结果 ===")
	log.Printf("总记录数: %d", totalCount)
	log.Printf("有content的记录数: %d", withContentCount)
	log.Printf("更新比例: %.1f%%", float64(withContentCount)/float64(totalCount)*100)
}